﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Poco;

namespace Metier
{
    public class GestionDevice
    {
        public void Ajouter(Device device)
        {
            try
            {
                using (Context db = new Context())
                {
                    device.DateCreation = DateTime.Now;
                    db.Utilisateurs.AddObject(device);
                    db.SaveChanges();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public Device Chercher(string email)
        {
            try
            {
                using (Context db = new Context())
                {
                    return (from u in db.Utilisateurs.OfType<Device>()
                            where u.Email == email
                            select u).FirstOrDefault();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw new Exception("Erreur Metier : Echec Recuperation Device : email est null", ex);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public List<Device> Lister(Restaurant restaurant)
        {
            try
            {
                using (Context db = new Context())
                {
                    return (from d in db.Utilisateurs.OfType<Device>()
                            where d.Restaurant.Id == restaurant.Id
                            select d).ToList();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void MiseAJour(Device device)
        {
            try
            {
                using (Context db = new Context())
                {
                    db.Utilisateurs.Attach(device);
                    db.ObjectStateManager.ChangeObjectState(device, System.Data.EntityState.Modified);
                    db.SaveChanges();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Supprimer(Device device)
        {
            try
            {
                using (Context db = new Context())
                {
                    db.Utilisateurs.DeleteObject(device);
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
